<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <meta name="viewport" content="width=device-width, initial-scale=1">
<#include "../common/header.ftl">
    <link rel="stylesheet" href="/js/plugins/treeview/bootstrap-treeview.min.css" type="text/css"/>
    <script type="text/javascript" src="/js/plugins/treeview/bootstrap-treeview.min.js"></script>
    <script>
        $(function () {
            $.get("/region/getParentId.do", function (data) {
                $("#tree").treeview({
                    data: [{text: '全部地区', nodes: data}],
                    //开启标签显示
                    showTags: true,

                    //懒加载
                    lazyLoad: function (node) {
                        //查询子节点数据
                        $.get("/region/getParentId.do", {id: node.id}, function (list) {
                            $("#tree").treeview('addNode', [list, node]);
                        })
                    },

                    //选择节点事件
                    onNodeSelected: function (event, data) {
                        //查询子节点数据
                        $.get('/region/listChildRegion.do', {id: data.id}, function (list) {
                            var temp = "";

                            $.each(list, function (index, ele) {
                                var btnText = "设为推荐";
                                if (ele.state == 1) {
                                    btnText = "取消推荐";
                                }
                                temp += "<tr><td>" + (index + 1) + "</td><td>" + ele.name + "</td>"
                                        + '<td><a href="javascript:;" class="editBtn" data-json=' + ele.jsonString + '>修改</a></td>'
                                        + '<td><a href="javascript:;" class="changeStateBtn" data-json=' + ele.jsonString + '>' + btnText + '</a></td>'
                                        + "<tr>";
                            })
                            $("tbody").html(temp);
                            //html之后让编辑按钮绑定点击事件
                            $(".editBtn").click(function () {
                                $("#editForm")[0].reset();
                                var json = $(this).data('json');
                                $("#regionId").val(json.id);
                                $("#name").val(json.name);
                                $("#parentId").val(json.parentId);
                                $("#parentName").val(json.parentName);
                                $("#regionModal").modal('show');
                            })
                            //修改状态按钮绑定点击事件
                            $(".changeStateBtn").click(function () {
                                var json = $(this).data('json');
                                var temp = 0;//普通
                                if (json.state == 0) {
                                    temp = 1;
                                }
                                $.post("/region/updateState.do", {id: json.id, state: temp}, function (data) {
                                    showMessager(data);
                                })
                            });

                        })
                    }
                });

                //模态框保存按钮
                $("#saveBtn").click(function () {
                    $("#editForm").ajaxSubmit(function (data) {
                        showMessager(data);
                    })
                })
                //添加地区按钮
                $(".addBtn").click(function () {
                    $("#editForm")[0].reset();
                    //获取被选中的节点
                    var arr = $("#tree").treeview("getSelected");
                    //选中之后对上级地区进行回显,name对应的是text
                    if (arr.length > 0) {
                        $("#parentId").val(arr[0].id);
                        $("#parentName").val(arr[0].text);
                    }
                    $("#regionModal").modal('show');
                })
            })
        })
    </script>
</head>
<body>

<div class="container " style="margin-top: 20px">
<#include "../common/top.ftl">
    <div class="row">
        <div class="col-sm-3">
        <#include "../common/menu.ftl">
        </div>
        <div class="col-sm-9">
            <div class="row">
                <div class="col-sm-12">
                    <h1 class="page-head-line">地区管理</h1>
                    <div class="form-group">
                        <a class="btn btn-success addBtn">添加</a>
                    </div>
                </div>
            </div>
            <div class="row">
                <div class="col-sm-4">
                    <div id="tree"></div>
                </div>
                <div class="col-sm-8">
                    <table class="table table-striped table-hover">
                        <thead>
                        <tr>
                            <th>序号</th>
                            <th>名称</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="regionModal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">旅游地区新增/编辑</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/region/saveOrUpdate.do" method="post" id="editForm">
                    <input type="hidden" id="regionId" name="id">
                    <div class="form-group">
                        <label for="name" class="col-sm-4 control-label">地区名称：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" id="name" name="name">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="sn" class="col-sm-4 control-label">上级地区：</label>
                        <div class="col-sm-6">
                            <input type="hidden" class="form-control" id="parentId" name="parent.id">
                            <input type="text" class="form-control" id="parentName" name="parent.name" readonly>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="saveBtn">保存</button>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->
</body>
</html>